<?php
/**
 * Created by PhpStorm.
 * User: shenyvlin
 * Date: 2019/2/27
 * Time: 15:56
 */
namespace app\api\model\user;

use think\Model;
use think\Db;
class Ordermodel extends Model{

    /**
     * 查询是否存在未支付订单
     */
    public function getOrder($order_id,$uid){
        $res = Db::name('order_info')->where([
            'user_id' => $uid,
            'id' => $order_id
        ])->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 创建订单
     */
    public function createOrder($data){
        $res = Db::name('order_info')->insertGetId($data);
        if($res){
            $info = db('order_info')->where('id',$res)->find();
            return $info;
        }else{
            return false;
        }
    }

    /**
     * 下单减库存
     */
    public function takeStock($sku,$gid,$sku_id){
        if(false !== $sku){
            db('goods_info_sku')->where('id',$sku_id)->setDec('stock');
        }else{
            db('goods_info')->where('id',$gid)->setDec('stock');
        }
    }

    /**
     * 订单详情
     */
    public function orderDetail($order_id,$uid){
        $where = [
            'id' => $order_id,
            'user_id' => $uid
        ];
        $order = db('order_info')->where($where)->find();
        if (!$order) {
            return false;
        }
        $goods_list = db('order_goods')->field('goods_id,sku_id,goods_name,goods_pic,goods_num,sku_name,sku_price')->where('order_id', $order_id)->select();
        $order['goods'] = $goods_list;
        return $order;
    }

    /**
     * 取消订单
     */
    public function cancelOrder($order_id){
        $res = db('order_info')->where('id',$order_id)->update(['order_status'=>5, 'cancel_status'=>1, 'cancel_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除订单
     */
    public function closeOrder($order_id)
    {
        $res = db('order_info')->where('id', $order_id)->update(['order_status' => 6]);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 确认收货
     */
    public function verifyReceive($order_id)
    {
        $res = db('order_info')->where('id', $order_id)->update(['order_status' => 4]);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取认证商品信息
     * @return array|bool|null|\PDOStatement|string|Model
     */
    public function getAuthProduct(){
        $res = db('lj_product')->where('product_flag','auth')->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getGoldProduct(){
        $where = [
            'product_flag' => 'gold',
            'del' => 0
        ];
        $res = db('lj_product')->where($where)->select();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 检测登录
     * @return bool
     */
    public function checkLogin(){
        $uid = $this->user['id'];
        $mobile = session('mobile');
        if($uid && $mobile){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询邀请码真伪
     */
    public function checkInvitationCode($code){
        $res = db('lj_users')->where('invitation_code',$code)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 填写邀请码
     */
    public function writeInvitationCode($code_uid,$uid){
        $data = [
            'invitation_user'=>$code_uid,
            'auth' => 1
            ];
        $res = db('lj_users')->where('id',$uid)->update($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    // 保存/更新 用户微信信息
    public function saveWxQr($data){
        $is_have = db('lj_users_wxqr')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_wxqr')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_wxqr')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据id查询用户微信
     */
    public function getUserWxQrInfo($uid){
        $res = db('lj_users_wxqr')->where('uid',$uid)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    // 提交微信审核
    public function getWxQrVerify($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_wxqr')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }
    // 提交微信上架
    public function getWxQrUp($data){
        $info = ['is_up'=>$data['type']];
        $res = db('lj_users_wxqr')->where('uid',$data['uid'])->update($info);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveWorkInfo($data){
        $is_have = db('lj_users_work')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_work')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_work')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserWorkInfo($uid){
        $res = db('lj_users_work')->where('uid',$uid)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    // 提交职业审核
    public function getUserWorkVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_work')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }
    public function getEducation(){
        $res = db('lj_education')->select();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveEduInfo($data){
        $is_have = db('lj_users_edu')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_edu')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_edu')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserEduInfo($uid){
        $res = db('lj_users_edu')->alias('e')
            ->field('e.*,edu.name as edu_name')
            ->where('e.uid',$uid)
            ->join('lj_education edu','edu.id = e.edu_id')
            ->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getUserEduVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_edu')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveCarInfo($data){
        $is_have = db('lj_users_car')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_car')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_car')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserCarInfo($uid){
        $res = db('lj_users_car')->where('uid',$uid)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getUserCarVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_car')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveHouseInfo($data){
        $is_have = db('lj_users_house')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_house')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_house')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function getUserHouseInfo($uid){
        $res = db('lj_users_house')
            ->alias('h')
            ->field('h.*,p.areaName province_name,c.areaName city_name,t.areaName county_name')
            ->where('h.uid',$uid)
            ->join('lj_china p','p.areaId = h.province')
            ->join('lj_china c','c.areaId = h.city')
            ->join('lj_china t','t.areaId = h.county')
            ->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function getUserHouseVerifyInfo($uid){
        $data = [
            'status' => 1,
            'verify_time' => date('Y-m-d H:i:s',time())
        ];
        $res = db('lj_users_house')->where('uid',$uid)->update($data);
        if($res){
            return $res;
        }else{
            return false;
        }
    }

    public function saveMarryInfo($data){
        $is_have = db('lj_users_marry')->where('uid',$data['uid'])->find();
        if($is_have){
            $res = db('lj_users_marry')->where('uid',$data['uid'])->update($data);
        }else{
            $res = db('lj_users_marry')->insert($data);
        }
        if($res){
            return true;
        }else{
            return false;
        }
    }
}